//
//  Utility.h
//  Dungeons and Princesses
//
//  Created by John Grange on 10/15/11.
//  Copyright (c) 2011 SD Networks. All rights reserved.
//

#ifndef Dungeons_and_Princesses_Utility_h
#define Dungeons_and_Princesses_Utility_h

#include <sstream>
#include <string.h>
#include <algorithm>
#include "Inventory.h"
#include <errno.h> // for errno
#include <sys/stat.h> // for stat and mkdir

#include <stdio.h>
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif

class Utility {
public:
    static string lowerCase(std::string stringToConvert)
    {
        std::string lowerCaseString = stringToConvert;
        transform (lowerCaseString.begin(), lowerCaseString.end(), lowerCaseString.begin(), ::tolower);

        return lowerCaseString;


    }

    static void getCommands(std::string commandToParse, vector<string> &commandsToPassBack) {

        //string command[size];
        string subCommand;
        istringstream iss(commandToParse);
        //int count = 0;
        while (getline (iss, subCommand, ' '))
        {
            commandsToPassBack.push_back(subCommand);

        }

    }

    static string removeGarbage(std::string stringToStrip) {

        stringToStrip.erase(stringToStrip.find_last_not_of(" \n\r\t")+1);

        return stringToStrip;
    }

    static void printListingOBject(Listing invObject) {

        std::cout << invObject.description << std::endl;
    }


    static string getCurrentWorkingDir()
    {
        char cCurrentPath[FILENAME_MAX];

        if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
        {
            throw "Error: something wierd has happened";
        }

        // ----------------------------------------------------------------------------
        // This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
        #ifdef __APPLE__
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
        //char path[PATH_MAX];
        if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)cCurrentPath, PATH_MAX))
        {
            // error!
        }
        CFRelease(resourcesURL);

        chdir(cCurrentPath);
        std::cout << "Current Path: " << cCurrentPath << std::endl;
        #endif
        // ----------------------------------------------------------------------------

        return cCurrentPath;
    }

    static bool createDir(std::string dirToCreate) {


        #ifdef __APPLE__
        struct stat st;
        if (stat(dirToCreate.c_str(), &st) == 0) {
            printf("%s already exists\n", dirToCreate.c_str());
            return EXIT_SUCCESS;
        }
        if (mkdir(dirToCreate.c_str(), S_IRWXU|S_IRWXG) != 0) {
            printf("Error creating directory: %s\n", strerror(errno));
            return EXIT_FAILURE;
        }
        printf("%s successfully created\n", dirToCreate.c_str());
        return EXIT_SUCCESS;
        #endif
    }


};
#endif
